package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzMachineMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzResultMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzSettingMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzSettingOutCheckMapper;
import com.jy.adscm.operation.mscyb.service.JqzzResultService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-02
 */
@Service
public class JqzzResultServiceImpl extends ServiceImpl<JqzzResultMapper, JqzzResult> implements JqzzResultService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JqzzMachineMapper jqzzMachineMapper;
    @Autowired
    JqzzSettingOutCheckMapper jqzzSettingOutCheckMapper;
    @Autowired
    JqzzSettingMapper jqzzSettingsMapper;
    @Override
    public boolean updateJqzzResult(List<JqzzResult> jqzzResult, Token token) throws AdscmSystemException {
        for (JqzzResult result : jqzzResult) {
            if ("检验结果".equals(result.getName())){
                JqzzMachine jqzzMachine = jqzzMachineMapper.selectById(result.getPid());
                if ("1".equals(result.getValue())){
                    jqzzMachine.setState(1);
                }
                if ("0".equals(result.getValue())){
                    jqzzMachine.setState(0);
                }
                jqzzMachineMapper.updateById(jqzzMachine);
            }

            boolean b = super.updateById(result);
        }

        return true;
    }

    @Override
    public List<JqbzResultContainer> selectByPid(String pid) throws AdscmSystemException {
        List<JqbzResultContainer> jqbzResultContainers =new ArrayList<>();
        Wrapper<JqzzResult> jqzzResultWrapper =new EntityWrapper<>();
        jqzzResultWrapper.eq("pid",pid);
        jqzzResultWrapper.orderBy("idx",false);
        jqzzResultWrapper.last("LIMIT 1");
        List<JqzzResult> jqzzResults = super.selectList(jqzzResultWrapper);
        Integer idx=1;
        if (jqzzResults!=null && jqzzResults.size()>0){
            JqzzResult jqzzResult = jqzzResults.get(0);
            idx= jqzzResult.getIdx();
        }
        for (int i = 1; i <=idx; i++) {
            Wrapper<JqzzResult> wrapper =new EntityWrapper<>();
            wrapper.eq("pid",pid);
            wrapper.eq("idx",i);
            wrapper.orderBy("rank",false);
            List<JqzzResult> jqzzResults1 = super.selectList(wrapper);
            if (jqzzResults1!=null && jqzzResults1.size()>0){
                for (JqzzResult jqzzResult : jqzzResults1) {
                    if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzResult.getCreator(),null)){
                        JyUser jyUser = jyUserMapper.selectById(jqzzResult.getCreator());
                        jqzzResult.setCreatorName(jyUser.getName());
                    }
                }
                JqzzResult jqzzResult = jqzzResults1.get(0);
                JqbzResultContainer  jqbzResultContainer=new JqbzResultContainer();
                JqzzMachine jqzzMachine = jqzzMachineMapper.selectById(pid);
                JqzzSetting jqzzSetting = jqzzSettingsMapper.selectById(jqzzMachine.getResourcename());
                jqbzResultContainer.setName(jqzzSetting.getName()+"检测记录");
                jqbzResultContainer.setCreator(jqzzResult.getCreatorName());
                jqbzResultContainer.setCreateTime(jqzzResult.getCreateTime());
                jqbzResultContainer.setJqzzResults(jqzzResults1);
                jqbzResultContainers.add(jqbzResultContainer);
            }
        }
        return jqbzResultContainers;
    }

    @Override
    public boolean insertJqzzResult(JqzzResult jqzzResult) {
        JqzzMachine jqzzMachine = jqzzMachineMapper.selectById(jqzzResult.getPid());
        Wrapper<JqzzSettingOutCheck> wrapper =new EntityWrapper<>();
        wrapper.eq("pid",jqzzMachine.getResourcename());
        wrapper.eq("isEnable",1);
        List<JqzzSettingOutCheck> jqzzSettingOutChecks = jqzzSettingOutCheckMapper.selectList(wrapper);
        //获取最大的编号
        Wrapper<JqzzResult> jqbzResultWrapper =new EntityWrapper<>();
        jqbzResultWrapper.eq("pid",jqzzResult.getPid());
        jqbzResultWrapper.orderBy("idx",false);
        jqbzResultWrapper.last("LIMIT 1");
        List<JqzzResult> jqzzResults = super.selectList(jqbzResultWrapper);
        Integer idx=1;
        if (jqzzResults!=null && jqzzResults.size()>0){
            JqzzResult jqzzResult1 = jqzzResults.get(0);
            idx=jqzzResult1.getIdx()+1;
        }
        for (JqzzSettingOutCheck jqzzSettingOutCheck : jqzzSettingOutChecks) {
            JqzzResult jqzzResult1=new JqzzResult();
            jqzzResult1.setIdx(idx);
            jqzzResult1.setPid(jqzzResult.getPid());
            jqzzResult1.setCid(jqzzSettingOutCheck.getId());
            jqzzResult1.setName(jqzzSettingOutCheck.getName());
            jqzzResult1.setReference(jqzzSettingOutCheck.getReference());
            jqzzResult1.setType(jqzzSettingOutCheck.getType());
            jqzzResult1.setUnit(jqzzSettingOutCheck.getUnit());
            jqzzResult1.setRemark(jqzzSettingOutCheck.getRemark());
            jqzzResult1.setCreator(jqzzResult.getCreator());
            jqzzResult1.setCreateTime(new Date());
            jqzzResult1.setRank(jqzzSettingOutCheck.getRank());
            super.insert(jqzzResult1);
        }
        return true;
    }
}
